/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include <iostream>
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "stub.h"
#include "utils/CertMgr.h"

using ::testing::_;
using testing::AllOf;
using ::testing::AnyNumber;
using ::testing::AtLeast;
using testing::ByMove;
using testing::DoAll;
using ::testing::Eq;
using ::testing::Field;
using ::testing::Ge;
using ::testing::Gt;
using testing::InitGoogleMock;
using ::testing::Invoke;
using ::testing::Ne;
using ::testing::Return;
using ::testing::Sequence;
using ::testing::SetArgReferee;
using ::testing::SetArgumentPointee;
using ::testing::Throw;

class CertMgrTest : public testing::Test {
public:
    void SetUp();
    void TearDown();
    static void SetUpTestCase() {}
    static void TearDownTestCase() {}
    Stub stub;
};

void CertMgrTest::SetUp()
{}

void CertMgrTest::TearDown()
{}

static AppProtect::ApplicationEnvironment SetUpProtectEnv()
{
    AppProtect::ApplicationEnvironment protectEnv;
    protectEnv.extendInfo = "{\"verifyStatus\":\"1\"}";
    protectEnv.id = "Id123";
    protectEnv.auth.extendInfo = "{\"fingerPrint\":\"4928EBB6E58A3A395BC89317DD55B82F1B8C10E7\""
                        ",\"enableCert\":\"1\""
                        ",\"certification\":\"-----BEGIN CERTIFICATE REQUEST-----\nMIIClDCCAXwCAQAwTzELMAkGA1UEBhMCQ04xDzANBgNVBAoMBkh1YXdlaTEQMA4G\nA1UECwwHU3RvcmFnZTEdMBsGA1UEAwwUMjEwMjM1NE5MRzEwTTgwMDAwMDMwggEi\nMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC3z3GXR90mlkdWZZ0Jq+pekikM\nAK6nIpm1/fHNm0lC3c2pzy2d7SQs7OJYwgaarCjgdEQj67mkEpXBI4IzND/2awo1\nRUYxD269lTgNIHSajkLAt4zgpUFSRvjcCc9YUFTzAdYcwWuzYOhGTVrxMjZhtNE3\nTCtzV/oMMpFOsj21CwxIP2kRX6zbMlYK88vtPQfGDAMI8IdKxSDNv5/zTYYwEjaz\nb3p8Y9TVBtDhjhvrsuNfxndBRtOCj6+BJdx//IG52mRTbAXLtpMlyMrWsdv5cKeU\nh/wJKMCB+cBGqNtSrOnP9FPE/3HEDLbhj/aShxcrTbQRW9pFotcnjKhvEa3/AgMB\nAAGgADANBgkqhkiG9w0BAQsFAAOCAQEAnE/5KWlLCD0ycfs51Xepjd9SPF2T3W7n\nEQtd09/6ZHefgBQyhjUotFqFFWI335zsG4afC4QxnJOzsg09U+sapM6lbw07o2h1\nfFohopWfuYTbcNkx+w1pNB5utKCBog/cT1Ei+JnlhSso6UikP0CP++FTxM8hZK9X\nGB/NNlB2ghECWPffm6pop1kpDz7svXWy8/FWJSP1EevslACy1JOjedf6V2RSzZEp\nm2yKk0d4Rz2MY+qos8axvdSYmsUTOG6YyFtTwH5H854kYBLZbbsUi9IB/Op9Vryb\npg7e2AIgIjq7xa80j3gdjyvNaFlgLVl6HgWvNb14Ub4W1O2czYxPAg==\n-----END CERTIFICATE REQUEST-----\n\""
                        ",\"revocationlist\":\"-----BEGIN X509 CRL-----\nMIIBtDCBnQIBATANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJDTjEKMAgGA1UE\nCAwBLjEKMAgGA1UEBwwBLjEPMA0GA1UECgwGaHVhd2VpMQ0wCwYDVQQDDARyb290\nFw0yMjA3MjcwNjUwNDdaFw0yMjA4MjYwNjUwNDdaMBQwEgIBBRcNMjIwNzI3MDY1\nMDE1WqAOMAwwCgYDVR0UBAMCAQAwDQYJKoZIhvcNAQELBQADggEBAHEd0FvGY/Yx\nvMtvZGXcNCSGO9Ij21Gx27UQ83NBEKCd1ndOc/yVhMbctUOeox1obs1Qv/M58A4U\n4/5cwInBBtS5BfNPL4cfwQwO1pvhftpAF8JPB9+D9cCaoVl6B6G5WzQN6Kp9p87l\nlvKEZo61Xq4VqXkDgyMuXK0Cenf9ZqG7/0wdq+p6gdVF0qabYjSVnGF1zZTFgKkd\nk3RaPvdbIO8xjno2p6lUVnrptNRQH+KOszlOBRGs/Ry0YrPSDK+otul2ci6+jISU\nDEbSNcUVQFRax3O/E4sez4W4/5XZYp4G2b8tr8OkU9yLxOgpyZDDRoFLtEQY4xvF\n04dn+s/K08M=\n-----END X509 CRL-----\n\"}";
    return protectEnv;
}

TEST_F(CertMgrTest, InitInfo)
{
    AppProtect::ApplicationEnvironment protectEnv = SetUpProtectEnv();
    CertMgr certMgrObj;
    std::string jobId = "123456";
    EXPECT_TRUE(certMgrObj.InitInfo(protectEnv, jobId));  // 校验证书，返回true
    EXPECT_TRUE(certMgrObj.IsVerify());
    protectEnv.extendInfo = "{\"verifyStatus\":\"0\"}";
    EXPECT_TRUE(certMgrObj.InitInfo(protectEnv, jobId));  // 不校验证书，返回true
}

TEST_F(CertMgrTest, InitInfo_Failed)
{
    AppProtect::ApplicationEnvironment protectEnv = SetUpProtectEnv();
    protectEnv.auth.extendInfo = "";
    CertMgr certMgrObj;
    std::string jobId = "123456";
    EXPECT_FALSE(certMgrObj.InitInfo(protectEnv, jobId));  // auth.extendInfo为空
    protectEnv.extendInfo = "";
    EXPECT_FALSE(certMgrObj.InitInfo(protectEnv, jobId));  // protectEnv.extendInfo为空
}

TEST_F(CertMgrTest, FlushCertificationToDisk)
{
    AppProtect::ApplicationEnvironment protectEnv = SetUpProtectEnv();
    CertMgr certMgrObj;
    std::string jobId = "123456";
    certMgrObj.InitInfo(protectEnv, jobId);
    std::string certPath = certMgrObj.GetCertPath();
    EXPECT_TRUE(certMgrObj.FlushCertificationToDisk());
}

TEST_F(CertMgrTest, FlushRevocationListToDisk)
{
    AppProtect::ApplicationEnvironment protectEnv = SetUpProtectEnv();
    CertMgr certMgrObj;
    std::string jobId = "123456";
    certMgrObj.InitInfo(protectEnv, jobId);
    std::string crlPath = certMgrObj.GetCrlPath();
    EXPECT_TRUE(certMgrObj.IncludeRevocationList());
    EXPECT_TRUE(certMgrObj.FlushRevocationListToDisk());
    EXPECT_TRUE(certMgrObj.RemoveCertAndRevocationListFromDisk());
}

TEST_F(CertMgrTest, SaveFile_Fail)
{
    CertMgr certMgrObj;
    std::string filePath = "/opt/oss";
    std::string content = "info";
    int cycleNums = 0;
    EXPECT_FALSE(certMgrObj.SaveFile(filePath, content, cycleNums));
}


